/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.server.mapper.AreaOfEmployMapper;
import com.ybkj.daijia.server.mapper.ChargeStartTimeOfEmployMapper;
import com.ybkj.daijia.server.mapper.CompanyMapper;
import com.ybkj.daijia.server.model.AreaOfEmploy;
import com.ybkj.daijia.server.model.ChargeStartTimeOfEmploy;
import com.ybkj.daijia.server.service.AreaOfEmployService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author Shine
 *
 */
@Service
public class DefaultAreaOfEmployService implements AreaOfEmployService {

    @Autowired
    private AreaOfEmployMapper areaOfEmployMapper;

    @Autowired
    private ChargeStartTimeOfEmployMapper chargeStartTimeOfEmployMapper;

    @Autowired
    private CompanyMapper companyMapper;


    @Transactional(rollbackFor = Exception.class)
    public AreaOfEmploy save(AreaOfEmploy area) {

        areaOfEmployMapper.insertRecord(area);

        if (null != area.getChargeStartTimes()) {
            for (ChargeStartTimeOfEmploy cst : area.getChargeStartTimes()) {
                if (!cst.isEmpty()) {
                    cst.setAreaId(area.getId());
                    chargeStartTimeOfEmployMapper.insertRecord(cst);
                }
            }
        }

        return area;
    }

    @Transactional(rollbackFor = Exception.class)
    public AreaOfEmploy update(AreaOfEmploy area) {

        AreaOfEmploy local = areaOfEmployMapper.selectByPrimaryKey(area.getId());

        //修改地区名称
        local.setName(area.getName());
        areaOfEmployMapper.updateByPrimaryKey(area);

        chargeStartTimeOfEmployMapper.deleteByArea(area.getId());

        if (!CollectionUtils.isEmpty(area.getChargeStartTimes())) {
            for (ChargeStartTimeOfEmploy cst : area.getChargeStartTimes()) {
                if (!cst.isEmpty()) {

                    cst.setAreaId(area.getId());
                    chargeStartTimeOfEmployMapper.insertRecord(cst);

                }
            }
        }

        return local;
    }

    public AreaOfEmploy findOne(Long id) {

        if (null == id) {
            return null;
        }

        return areaOfEmployMapper.selectByPrimaryKey(id);
    }

    public List<AreaOfEmploy> findByIdIn(Long[] ids) {

        return areaOfEmployMapper.selectByIdIn(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Long areaId) {

        // 删除时间段
        chargeStartTimeOfEmployMapper.deleteByArea(areaId);
        // 删除地区信息
        areaOfEmployMapper.deleteByPrimaryKey(areaId);

    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteChargeStartTimesByArea(Long areaId) {
        // 删除时间段
        chargeStartTimeOfEmployMapper.deleteByArea(areaId);
    }


    @Override
    public AreaOfEmploy findOneWithCharge(Long areaId) {
        if (null == areaId) {
            return null;
        }
        return areaOfEmployMapper.selectByPrimaryKeyWithCharge(areaId);
    }

    @Override
    public List<AreaOfEmploy> findByEmployWithCharge(Long employId) {

        return areaOfEmployMapper.selectByEmployWithCharge(employId);
    }

    @Override
    public AreaOfEmploy findByNameAndEmploy(String name, Long employId) {

        return areaOfEmployMapper.selectByNameAndEmploy(name, employId);
    }


}